﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MotionCS.Helper;
namespace MotionCS.Controller
{
    public class Dm2cAxis : IAxis
    {
        /// <summary>
        /// 轴配置
        /// </summary>
        private AxisConfi config;
        public AxisConfi Config { get => config; }
        /// <summary>
        /// 左到右
        /// </summary>
        private ushort LRDoBit;
        /// <summary>
        /// 右到左
        /// </summary>
        private ushort RLDoBit;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="axisConfi"></param>
        public Dm2cAxis(AxisConfi axisConfi)
        {
            config = axisConfi;
            LRDoBit = (ushort)((config.Axis / 10) % 10);
            RLDoBit = (ushort)(config.Axis % 10);
            InitConfi();
        }
        /// <summary>
        /// 初始化配置
        /// </summary>
        private void InitConfi()
        {
            int spd = Convert.ToInt32(config.AutoSpeed) * 60 / (int)config.OutUnit;
            int acc = Convert.ToInt32(config.AutoSpeed / config.AutoAccel) * 1000 * 1000 / spd;
            int dec = Convert.ToInt32(config.AutoSpeed / config.AutoDecel) * 1000 * 1000 / spd;
            Dm2cApi.Instance.SetSpeed(1, (ushort)spd);
            Dm2cApi.Instance.SetSpeed(2, (ushort)spd);
            Dm2cApi.Instance.SetAccel(1, (ushort)acc);
            Dm2cApi.Instance.SetAccel(2, (ushort)acc);
            Dm2cApi.Instance.SetDecel(1, (ushort)dec);
            Dm2cApi.Instance.SetDecel(2, (ushort)dec);
        }
        /// <summary>
        /// 设置手动速度
        /// </summary>
        public void SetMualSpeed()
        {
            //Dm2cApi.Instance.SetAccel(Axis, (ushort)(1000 * ManualAccel / ManualSpeed));
            //Dm2cApi.Instance.SetDecel(Axis, (ushort)(1000 * ManualAccel / ManualSpeed));
            //Dm2cApi.Instance.SetSpeed(Axis, (ushort)(ManualSpeed * Unit));
        }
        /// <summary>
        /// 设置自动速度
        /// </summary>
        public void SetAutoSpeed()
        {
            //Dm2cApi.Instance.SetAccel(Axis, (ushort)(AutoAccel / Unit));
            //Dm2cApi.Instance.SetDecel(Axis, (ushort)(AutoDecel / Unit));
            //Dm2cApi.Instance.SetSpeed(Axis, (ushort)(AutoSpeed / Unit));
        }
        public void SetAutoSpeed2()
        {

        }
        /// <summary>
        /// 进入回零速度
        /// </summary>
        public void SetHomeSpeed()
        {
            
        }
        /// <summary>
        /// 获取轴运行状态
        /// </summary>
        /// <returns>true-停止 false-运动中</returns>
        public bool IsCheckDone() 
        {
            return Smc300Api.ReadOutbit(LRDoBit) == 1 
                && Smc300Api.ReadOutbit(RLDoBit) == 1;
        }
        /// <summary>
        /// 运动到绝对点
        /// </summary>
        /// <param name="fdis"></param>
        public int MoveAbs(double fdis) 
        {
            return 0;
        }
        /// <summary>
        /// 运动到相对点
        /// </summary>
        /// <param name="fdis"></param>
        public int MoveRel(double fdis) 
        {
            return 0;
        }
        /// <summary>
        /// 前向运动
        /// </summary>
        /// <param name="dir"></param>
        public int MoveForward() 
        {
            if (config.MoveDir == 0)
            {
                Smc300Api.WriteOutbit(RLDoBit, 1);
                Smc300Api.WriteOutbit(LRDoBit, 0);
            }
            else
            {
                Smc300Api.WriteOutbit(LRDoBit, 1);
                Smc300Api.WriteOutbit(RLDoBit, 0);
            }

            return 0;
        }
        /// <summary>
        /// 后向运动
        /// </summary>
        public int MoveBackward()
        {
            if (config.MoveDir == 0)
            {
                Smc300Api.WriteOutbit(LRDoBit, 1);
                Smc300Api.WriteOutbit(RLDoBit, 0);
            }
            else
            {
                Smc300Api.WriteOutbit(RLDoBit, 1);
                Smc300Api.WriteOutbit(LRDoBit, 0);
            }

            return 0;
        }
        /// <summary>
        /// 停止运动
        /// </summary>
        public int MoveStop() 
        {
            Smc300Api.WriteOutbit(RLDoBit, 1);
            Smc300Api.WriteOutbit(LRDoBit, 1);

            return 0;
        }
        /// <summary>
        /// 急停运动
        /// </summary>
        /// 
        public int EmgStop() 
        {
            Smc300Api.WriteOutbit(RLDoBit, 1);
            Smc300Api.WriteOutbit(LRDoBit, 1);
            return 0;
        }
        /// <summary>
        /// 回零运动
        /// </summary>
        public int MoveHome() 
        {
            return 0;
        }
        /// <summary>
        /// 获取轴状态
        /// </summary>
        /// <returns></returns>
        public MotorStatus GetStatus()
        {
            return new MotorStatus();
        }
        /// <summary>
        /// 是否到位
        /// </summary>
        /// <returns>true 到位 flase 未到位</returns>
        public bool IsInp() { return false; }
        /// <summary>
        /// 是否回零完成
        /// </summary>
        /// <returns>false 回零中 true 回零成功 </returns>
        public bool IsHomeFinish() { return false; }
        /// <summary>
        /// 是否在原点
        /// </summary>
        /// <returns></returns>
        public bool IsHomeInOn() { return false; }
        /// <summary>
        /// 是否触发硬件负限位
        /// </summary>
        /// <returns>false 未触发 true 触发</returns>
        public bool IsElRevLimit() { return false; }
        /// <summary>
        /// 是否触发硬件正限位
        /// </summary>
        /// <returns>false 未触发 true 触发</returns>
        public bool IsElFwdLimit() { return false; }
        /// <summary>
        /// 是否驱动器报警
        /// </summary>
        /// <returns>false 未报警 true 报警</returns>
        public bool IsAlarm() { return false; }
        /// <summary>
        /// 伺服使能
        /// </summary>
        public void SevonEnable() { }
        /// <summary>
        /// 伺服失能
        /// </summary>
        public void SevonDisable() { }
        /// <summary>
        /// 复位使能
        /// </summary>
        public void ResetEnable() { }
        /// <summary>
        /// 复位失能
        /// </summary>
        public void ResetDisable() { }
        /// <summary>
        /// 驱动器报警值
        /// </summary>
        /// <returns></returns>
        public Int32 GetAlmStatus()
        {
            //return MotionDevice.Instance.DiBit(DiE.T1MotorAlarm).Inbit() == 1 ? 0 : 1;//轴报警

            return 0;
        }
        /// <summary>
        /// 设置指令位置
        /// </summary>
        /// <param name="dpos"></param>
        public int SetPositionUnit(double dpos) { return 0; }
        /// <summary>
        /// 获取指令位置
        /// </summary>
        /// <returns></returns>
        public int GetPositionUnit(ref double dpos) { return 0; }
        /// <summary>
        /// 设置编码器反馈位置
        /// </summary>
        /// <param name="mpos"></param>
        public int SetEncoderUnit(double dpos) { return 0; }
        /// <summary>
        /// 获取编码器反馈位置
        /// </summary>
        /// <param name="mpos"></param>
        public int GetEncoderUnit(ref double mpos) { return 0; }
        /// <summary>
        /// 轴运动速度
        /// </summary>
        /// <param name="speed"></param>
        public int GetVpSpeed(ref double speed) { return 0; }
    }
}
